package com.ilovepostcard;

import com.ilovepostcard.utils.ImageUtil;
import com.ilovepostcard.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

public class LaunchCreateActivity extends Activity {

    public static final String LAUNCH_TYPE_KEY = "com.ilovepostcard.LaunchCreateActivity.LAUNCH_TYPE_KEY";
    public static final int LAUNCH_TYPE_CAMERA = 1;
    public static final int LAUNCH_TYPE_GALLERY = 2;
    
    Uri imageUri;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_create);
        
        Intent intent = getIntent();
        int type = intent.getIntExtra(LAUNCH_TYPE_KEY, 0);
        launch(type);
    }
    
    private void launch(int type) {
        if (type == LAUNCH_TYPE_CAMERA) {
        	imageUri = ImageUtil.getImageFromCamera(this);
        } else if (type == LAUNCH_TYPE_GALLERY) {
            Intent album = new Intent(Intent.ACTION_GET_CONTENT);
            album.setType("image/*");
            startActivityForResult(album, 1);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        
        if (resultCode != RESULT_OK) 
        {
            //Toast.makeText(this, R.string.error_selectImage, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        Uri uri = null;
        try {
            
            if (requestCode == ImageUtil.GALLERY) {
                uri = data.getData();
                
            } 
            else if (requestCode == ImageUtil.CAMERA)
            {
            	uri = imageUri;
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        
        
        if(uri!= null)
        {
            Intent intent = new Intent(this, EditImageActivity.class);
            Bundle bundle =new Bundle();
            String from = "camera";
            if(uri != null)
                from = "gallery";
            bundle.putString("type",from);
            bundle.putParcelable("uri", uri);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        finish();
    }
    
    
    
   /* private Bitmap getCameraBitmap()
    {
    	Uri selectedImage = imageUri;
    	getContentResolver().notifyChange(selectedImage, null);
    	ContentResolver cr = getContentResolver();
    	Bitmap bitmap = null;
    	try {
    	bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);

    	Toast.makeText(this, selectedImage.toString(),Toast.LENGTH_LONG).show();
    	} catch (Exception e) {
    	Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
    	}
    	
    	return bitmap;
    }
*/
}
